package com.example.controller;

import com.example.common.Result;
import com.example.entity.Account;
import com.example.entity.Repair;
import com.example.mapper.RepairMapper;
import com.example.mapper.UserMapper;
import com.example.service.UserRepairService;
import com.example.service.WorkerRepairService;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 账单表前端操作接口
 **/
@RestController
@RequestMapping("/worker/repair")
public class WorkerRepairController {

    @Resource
    private WorkerRepairService workerRepairService;
    @Resource
    private UserRepairService userRepairService;
    @Resource
    private RepairMapper repairMapper;
    @Resource
    private UserMapper userMapper;
    // 文件上传存储路径
    private static final String filePath = System.getProperty("user.dir") + "/files/";
    @Value("${server.port:9090}")
    private String port;
    @Value("${ip:localhost}")
    private String ip;

    /**
     * 新增
     */
    @PostMapping("/add")
    public Result add(@RequestBody Repair repair) {
        workerRepairService.add(repair);
        return Result.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        workerRepairService.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        workerRepairService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result updateById(@RequestBody Repair repair) {
        workerRepairService.updateById(repair);
        List<String> toUser = new ArrayList<>();
        // TODO 传消息给对应用户
        toUser.add(userMapper.selectById(repairMapper.selectById(repair.getId()).getUserId()).getUsername());
        userRepairService.notice(toUser, "您的维修单已处理");
        return Result.success();
    }

    /**
     * 不需要评价
     */
    @PostMapping("/evaluate")
    public Result evaluateById(@RequestBody Repair repair) {
        // TODO 添加评价前提判断 未评价分数会变低？
        workerRepairService.updateById(repair);
        return Result.success();
    }

    /**
     * 根据ID查询
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Repair bill = workerRepairService.selectById(id);
        return Result.success(bill);
    }

    /**
     * 查询所有
     */
    @GetMapping("/selectAll")
    public Result selectAll(Repair repair) {
        // TODO 筛选自己的维修单返回
        Account worker = TokenUtils.getCurrentUser();
        repair.setWorker1Id(worker.getId());
        repair.setWorker2Id(worker.getId());
        List<Repair> list = workerRepairService.selectAll(repair);
        return Result.success(list);
    }

    /**
     * 分页查询
     */
    @GetMapping("/selectPage")
    public Result selectPage(Repair repair,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        Account worker = TokenUtils.getCurrentUser();
        repair.setWorker1Id(worker.getId());
        repair.setWorker2Id(worker.getId());
        List<Repair> list = workerRepairService.selectAll(repair);
        PageInfo<Repair> page = workerRepairService.selectPage(repair, pageNum, pageSize);
        return Result.success(page);
    }

    // 统计账单的比例情况
    @GetMapping("/count")
    public Result count(String type) {
        List<Repair> list = workerRepairService.count(type);
        return Result.success(list);
    }

}